/**
 * 用户关联角色时选择角色的窗口。
 * @author linzongxue
 * @create_date 2012-1-31
 */
define(function(){
	var SelectRoleWindow = Ext.extend(Ext.Window, {
		title: '用户关联角色——选择角色',
		resizable: false,
		modal: true,
		width: 500,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle: 'padding:3px;',
		iconCls: 'icon-window',
		initComponent: function(){
			SelectRoleWindow.superclass.initComponent.call(Ext.apply(this, {
				tbar:[this.createSearchField()],
				items: [this.grid = this.createRoleGridPanel()],
				buttons: [
					{text: '确定', width:60, handler: this.ok, iconCls: 'icon-ok', scope: this},
					{text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this}
				]
			}));
			this.on('afterrender', function(){
				this.refresh();
			});
		},
		user: null, //要与角色关联的用户
		refresh: function(){
			var searchField = this.getTopToolbar().items.first();
			searchField.setValue(null);
			searchField.triggers[0].hide();
			this.grid.getStore().load();
		},
		ok: function(){
			var records = this.grid.getSelectionModel().getSelections();
			if (records.length == 0){
				cu.hint('请选择要关联的角色');
				return;
			}
			this.callback.call(this.cbScope, lu.pluck(records, 'id'));
			this.close();
		},
		findRoles: function(key){
			var grid = this.grid;
			grid.getStore().each(function(record, index){
				var found = record.get('name').toLowerCase().indexOf(key.toLowerCase()) > -1;
				grid.getView().getRow(index).style.display = found ? '' : 'none';
			});
		},
		createRoleGridPanel: function(){
			var sm = new Ext.grid.CheckboxSelectionModel();
			var grid = new Ext.grid.GridPanel({
				height: 300,
				border: false,
				stripeRows: true,
			    enableHdMenu: false,
			    autoExpandColumn: 3,
			    store: new Ext.data.JsonStore({
				    idProperty: 'id',
				    fields: ['name', 'memo'],
				    url: 'sys/user/getUnselectedRolesOfUser.do?userId=' + this.user.id
				}),
				sm: sm,
				columns: [
					new Ext.grid.RowNumberer(),
				    sm,
					{header: '名称', width: 150, dataIndex: 'name'},
					{header: '备注', width: 80, dataIndex: 'memo'}
				]
			});
			return grid;
		},
		createSearchField: function(){
			return new Ext.form.TwinTriggerField({
				trigger1Class: 'x-form-clear-trigger',
                trigger2Class: 'x-form-search-trigger',
                hideTrigger1: true,
                emptyText: '输入角色名称查找',
                style:'margin-left:5px;',
                onTrigger1Click: function(e){
                	this.ownerCt.ownerCt.refresh();
                },
                onTrigger2Click: function(e){
                	if (this.getValue() == ''){
                		this.ownerCt.ownerCt.refresh();
                	}
                	else{
                		this.ownerCt.ownerCt.findRoles(this.getValue());
                		this.triggers[0].show();
                	}
                },
                listeners: {
                	specialKey: function(field, e){ //回车搜索
                		if (e.getKey() == e.ENTER) this.onTrigger2Click();
                	}
                }
			});
		}
	});
	return SelectRoleWindow;
});